﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using DataAccessLayer.Entity;
using BussinessLayer;
using System.Security.Cryptography;
using System.Threading;
namespace QLTSLienThong.Form_he_thong
{
    public partial class frmLogin : Form
    {
        public frmLogin()
        {
            InitializeComponent();
        }
        TaiKhoanBussiness taikhoanBussiness = new TaiKhoanBussiness();
        clsTaiKhoan clsTK;
        DataTable dt;
        private void timer1_Tick(object sender, EventArgs e)
        {
            //circularProgress1.Value += 1;
            //if (circularProgress1.Value >= 100)
            //{
            //    circularProgress1.Value = 0; ;
            //    circularProgress1.Value += 1;
            //}
            int width = this.Width;
            Point pOld = lblTruoc.Location;
            if (pOld.X + lblTruoc.Width == 0)
                pOld.X = width + lblTruoc.Location.X;//khoi tao lai 
            Point pNew = new Point(pOld.X - 1, pOld.Y);
            lblTruoc.Location = pNew;

            Point pN = lblTruoc.Location;
            if (pN.X + lblTruoc.Width == 0)
                pN.X = width + lblTruoc.Location.X;
            Point pNewN = new Point(pN.X - 1, pN.Y);
            lblTruoc.Location = pNewN;
        }

        private void buttonX2_Click(object sender, EventArgs e)
        {
            Application.Exit();
        }

        private void frmLogin_FormClosing(object sender, FormClosingEventArgs e)
        {
            if (MessageBox.Show("Bạn muốn thoát khỏi hệ thống?", "Thông báo", MessageBoxButtons.YesNo, MessageBoxIcon.Question) == DialogResult.No)
                e.Cancel=true;
        }

        private void buttonX1_Click(object sender, EventArgs e)
        {
            timer2.Enabled = true;
            circularProgress1.Visible = true;
        }
        private String MaHoaMatKhau(String txt)
        {
            String str = "";
            byte[] buffer = System.Text.Encoding.UTF8.GetBytes(txt);
            System.Security.Cryptography.MD5CryptoServiceProvider md5 = new System.Security.Cryptography.MD5CryptoServiceProvider();
            buffer = md5.ComputeHash(buffer);
            foreach (byte b in buffer)
            {
                str += b.ToString("X2");
            }

            return str;
        }

        private void timer2_Tick(object sender, EventArgs e)
        {
            circularProgress1.Value += 1;
            if (circularProgress1.Value >= 100)
            {
                //Thread.Sleep(500);
                pictureBox1.ImageLocation = Application.StartupPath + "\\login.ico";
                //Thread.Sleep(300);
                timer2.Enabled = false;
                try
                {
                    clsTK = new clsTaiKhoan();
                    clsTK.TenDangNhap = txtUsername.Text.Trim();
                    clsTK.MatKhau = MaHoaMatKhau(txtPassWord.Text.Trim());
                    dt = new DataTable();
                    dt = taikhoanBussiness.Login(clsTK);
                    if (dt.Rows.Count > 0)
                    {
                        frmMain._Quyen = taikhoanBussiness.Login(clsTK).Rows[0][3].ToString().Trim();
                        frmMain f = new frmMain();
                        f.Show();
                        this.Hide();
                        frmMain._Username = txtUsername.Text.Trim();
                    }
                    else
                    {
                        MessageBox.Show("Tài khoản không tồn tại!", "Thông báo!");
                    }
                }
                catch (Exception)
                {

                    throw;
                }
            }
        }
    }
}
